package org.example.alig;

import java.util.ArrayList;
import java.util.List;

public class Alig1 {
    public static void main(String[] args) {
//        int datas[][] = new int[3][4];
        int datas[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
        int m = datas.length;
        int n = datas[0].length;
        System.out.println(m + " " + n);

        int i = 0, j = 0, di = 0;
        int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        List<Integer> list = new ArrayList<>(m * n);

        int top = 0;
        int bottom = m;
        int left = 0;
        int right = n;
        for (int k = 0; k < m * n; k++) {
            list.add(datas[i][j]);
            // 预测
            int x = i + DIRS[di][0];
            int y = j + DIRS[di][1];

            if (x < top || x >= bottom || y < left || y >= right) {
                switch (di) {
                    case 0:
                        top += 1;
                        break;
                    case 1:
                        right += 1;
                        break;
                    case 2:
                        bottom += 1;
                        break;
                    case 3:
                        left += 1;
                        break;
                }
                // 实现方向的循环切换 (右->下->左->上->右)
                di = (di + 1) % 4;
            }
            // 实际
            i = i + DIRS[di][0];
            j = j + DIRS[di][1];

        }
        System.out.println(list);
    }
}
